<template>
  <div class="goods_container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加分类 -->
      <el-button type="primary" class="addCate" @click="showAddCateDialog">添加分类</el-button>
      <!-- 表格区域 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
      >
        <template slot="isOk" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="ranking" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="option" slot-scope="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="editCate(scope.row)">编辑</el-button>
          <el-button
            icon="el-icon-delete"
            type="danger"
            size="mini"
            @click="deleteCate(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- dialog，添加分类对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="30%"
      :show-close="false"
      :modal="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addCateFormRef"
        :model="addCateForm"
        label-width="80px"
        :rules="addCateFormRules"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="选择分类">
          <div class="block">
            <!-- options指定数据源，props指定配置对象label、children是哪个 -->
            <el-cascader
              v-model="cascaderVal"
              :options="parentCateList"
              expandTrigger="hover"
              :props="props"
              @change="handleCascaderChange"
              change-on-select
              clearable
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAddCate">取 消</el-button>
        <el-button type="primary" @click="submitCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类名操作弹出框 -->
    <el-dialog title="提示" :visible.sync="editCateDialogVisible" width="30%">
      <el-form label-width="80px" :rules="addCateFormRules" :model="editCateForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类数据
      cateList: [],
      cascaderVal: [],
      // 获取分类数据查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      addCateDialogVisible: false,
      editCateDialogVisible: false,
      editCateForm: {
        cat_name: '',
        cat_id: ''
      },
      // 添加分类信息
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 父级分类,弹出框数据
      parentCateList: [],
      // 设置选择数据
      props: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      // 为table制定列的定义,每一个对象表示一列
      columns: [
        {
          label: '分类名',
          prop: 'cat_name'
        },
        {
          label: '是否可用',
          // 将当前列定义为模版列
          type: 'template',
          // 当前列的模板名称
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          // 当前列的模板名称
          template: 'ranking'
        },
        {
          label: '操作',
          type: 'template',
          // 当前列的模板名称
          template: 'option'
        }
      ],
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类数组
    getCateList() {
      this.$http.get(`categories`, { params: this.queryInfo }).then(res => {
        if (res.data.meta.status != 200) {
          return this.$message.error('获取商品分类失败')
        }
        this.cateList = res.data.data.result
        this.total = res.data.data.total
        // console.log(res.data.data.result)
      })
    },
    // 监听设置的每页显示，这个api已经默认传值了
    handleSizeChange() {},
    // 监听目前的页码,elementui已经默认传递了
    handleCurrentChange(currentPage) {
      this.queryInfo.pagenum = currentPage
      this.getCateList()
    },
    // 展示添加分类对话框
    showAddCateDialog() {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },
    // 获取父级分类
    getParentCateList() {
      // 传递参数需要params
      this.$http.get('categories', { params: { type: 2 } }).then(res => {
        if (res.data.meta.status != 200) {
          return this.$message.error('获取数据失败')
        }
        this.parentCateList = res.data.data
        // console.log(this.parentCateList)
      })
    },
    // 监听添加分类选择框的改变,给要提交的信息赋值
    handleCascaderChange() {
      if (this.cascaderVal.length > 0) {
        this.addCateForm.cat_pid = this.cascaderVal[this.cascaderVal.length - 1]
        return (this.addCateForm.cat_level = this.cascaderVal.length)
      }
      this.addCateForm.cate_pid = 0
      this.addCateForm.cate_level = 0
    },
    // 取消选择
    cancelAddCate() {
      this.addCateDialogVisible = false
      this.cascaderVal = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    // 提交新建的分类信息
    submitCate() {
      this.$refs.addCateFormRef.validate(valid => {
        if (!valid) {
          this.$message.closeAll()
          return this.$message.error('您未输入分类名称')
        }
        this.$http.post('categories', this.addCateForm).then(res => {
          console.log(res.data)
          if (res.data.meta.status != 201) {
            return this.$message.error('获取数据失败')
          }
          this.$message.success('传递成功')
          this.cascaderVal = []
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
          this.addCateForm.cat_name = ''
          this.getCateList()
        })
      })
    },
    // 编辑分类名显示
    editCate(data) {
      // this.editCateDialogVisible = true
      // console.log(data)
      // this.editCateForm.cat_id = data.cat_id
      this.$message.closeAll()
      this.$message('此功能正在完善')
    },
    // 提交编辑完成的分类名
    submitEditCate() {
      // console.log(this.editCateForm)
      // 这个api似乎有些问题
      this.$http
        .get(
          `categories/${this.editCateForm.cat_id}?cat_name=${this.editCateForm.cat_name}`
        )
        .then(res => {
          console.log(res)
        })
    },
    // 删除分类操作
    deleteCate(data) {
      console.log(data.cat_name)
      this.$http.delete(`categories/${data.cat_id}`).then(res => {
        if (res.data.meta.status != 200) {
          this.$message.closeAll()
          return this.$message.error('删除是百度')
        }
        this.getCateList()
      })
    }
  }
}
</script>

<style lang="less">
.goods_container {
  .el-card {
    margin-top: 20px;
    .addCate {
      margin-bottom: 10px;
    }
    .el-pagination {
      margin-top: 15px;
    }
  }
}
</style>